package com.zkingsoft.web.action;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.web.bean.WebNav;
import com.zkingsoft.web.service.WebNavService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-28 15:35
 */
@Controller
@RequestMapping(value = "admin/webNav")
public class WebNavController {

	@Resource
	private WebNavService webNavService;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "WebNav_BEV";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebNav webNav, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webNav.setCompanyId(user.getCompanyId());
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, webNavService.findInPage(webNav, pageVo),
				webNavService.findTotal(webNav));
		return result;
	}

	/**
	 * 新增或者修改页面
	 */
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebNav webNav) {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		
		if (webNav.getNavId() != null) {
			webNavService.modifyByModel(webNav);
			// 更新内存导航
			WebNav webNav2 = new WebNav();
			webNav2.setNavType("mobile");
			webNav2.setParentId(0L);
			webNav2.setNavShow(WebNav.SHOW);
			webNav2.setCompanyId(user.getCompanyId());
			List<WebNav> navlist = webNavService.findByModel(webNav2);
			WebUtil.setSessionAttribute("mobileNav", navlist);
			webNav2.setNavType("pc");
			navlist = webNavService.findByModel(webNav2);
			WebUtil.setSessionAttribute("pcNav", navlist);
			AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, "导航栏修改成功");
			return result;
		} else {
			webNav.setCompanyId(user.getCompanyId());
			int i = webNavService.add(webNav);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS, "导航栏新增成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL, "导航栏新增失败");
			}
		}
	}

	/**
	 * 进入修改界面
	 */
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		WebNav webNav;
		if (id != null) {
			webNav = webNavService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webNav);
			WebUtil.setSessionAttribute(BEV, webNav);
		}
		return "admin/web/nav-form";
	}

	/**
	 * 删除
 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = webNavService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "删除失败");
		}
	}
	
	@RequestMapping(value = "/findById")
	public @ResponseBody AjaxResult findById(Long id) {
		WebNav webNav = webNavService.findById(id);
		WebUtil.setSessionAttribute(BEV, webNav);
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, Arrays.asList(webNav), 0);
	}

	/**
	 * 查询所有的导航栏类型
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all(WebNav webNav) {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webNav.setCompanyId(user.getCompanyId());
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, webNavService.findByModel(webNav), 0);
	}

}